package dev.huidocs.docs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.coadmin.utils.QueryHelpMybatisPlus;
import com.gitee.coadmin.base.PageInfo;
import com.gitee.coadmin.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import dev.huidocs.docs.domain.DocResEmbed;
import dev.huidocs.docs.service.DocResEmbedService;
import dev.huidocs.docs.service.dto.DocResEmbedDTO;
import dev.huidocs.docs.service.dto.DocResEmbedQueryParam;
import dev.huidocs.docs.service.mapper.DocResEmbedMapper;
import dev.huidocs.docs.service.converter.DocResEmbedConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
 * @author jinjin
 * @since 2024-10-11
 */
@Service
@RequiredArgsConstructor
// @CacheConfig(cacheNames = DocResEmbedService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DocResEmbedServiceImpl implements DocResEmbedService {

    // private final RedisUtils redisUtils;
    private final DocResEmbedMapper docResEmbedMapper;
    private final DocResEmbedConverter docResEmbedConverter;

    @Override
    public PageInfo<DocResEmbedDTO> pageByParam(DocResEmbedQueryParam query, Pageable pageable) {
        IPage<DocResEmbed> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DocResEmbed> page = docResEmbedMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return docResEmbedConverter.convertPage(page);
    }

    @Override
    public List<DocResEmbedDTO> listByParam(DocResEmbedQueryParam query){
        return docResEmbedConverter.toDto(docResEmbedMapper.selectList(QueryHelpMybatisPlus.getPredicate(query, "id", false)));
    }

    @Override
    public long countByParam(DocResEmbedQueryParam query) {
        return docResEmbedMapper.selectCount(QueryHelpMybatisPlus.getPredicate(query));
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DocResEmbedDTO getById(Long id) {
        if (id == null) {
            return null;
        }
        return docResEmbedConverter.toDto(docResEmbedMapper.selectById(id));
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void insert(DocResEmbedDTO res) {
        DocResEmbed entity = docResEmbedConverter.toEntity(res);
        docResEmbedMapper.insert(entity);
        res.setId(entity.getId());
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DocResEmbedDTO res){
        DocResEmbed entity = docResEmbedConverter.toEntity(res);
        docResEmbedMapper.updateById(entity);
        // delCaches(res.id);
    }

    @Override
    // @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(Set<Long> ids){
        // delCaches(ids);
        docResEmbedMapper.deleteBatchIds(ids);
    }
    
    /*
    private void delCaches(Long id) {
        redisUtils.del(CACHE_KEY + "::id:" + id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/
}
